//
//  HomeTableViewController.swift
//  sina
//
//  Created by DXapple on 15/11/9.
//  Copyright © 2015年 sqlite. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
private let HomeCellId = "HomeCellId"

class HomeTableViewController: BaseTableViewController {
    
    private lazy var statuses = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !userLogin {
             visitorLoginView?.setUIInfo(nil, title: "关注一些人，回这里看看有什么惊喜")
            return
        }
       

        //注册id
        perpareTableView()
        //获取网络数据
        lodata()
    
    }

    
    //注册id
    private func perpareTableView() {
        refreshControl = UIRefreshControl()
        
        //添加监听方法
        refreshControl?.addTarget(self, action: "lodata", forControlEvents: .ValueChanged)
        
        
    tableView.registerClass(StatusCellTableViewCell.self, forCellReuseIdentifier: HomeCellId)

    tableView.estimatedRowHeight = 200
        
    tableView.rowHeight = UITableViewAutomaticDimension
        //线
        tableView.separatorStyle = .None
        tableView.tableFooterView = indicatorView

        
    }
    
    
    
    //获得数据
//  @objc private func lodata(finished: (array: [Status]?) ->() ) {
    @objc private func lodata(){
        
        var since_id = 0
        var max_id = 0
        if indicatorView.isAnimating() {
            since_id = 0
            max_id = statuses.last?.id ?? 0
            
        } else {
            max_id = 0
            //确定sice_id是多少
            since_id = statuses.first?.id ?? 0
        }
        

        StatusListViewModel().lodata(since_id: since_id, max_id: max_id) { (array) -> () in
            self.refreshControl?.endRefreshing()
            if array == nil {
                
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
                self.indicatorView.stopAnimating()

                return
            }
            if since_id > 0 {
                //如果不加入到数组那么数组中就没有数据就成空白了
                self.statuses  = array! + self.statuses

            } else if max_id > 0  {
                self.statuses += array!
                //下拉加载完成停止动画
                self.indicatorView.stopAnimating()

            }else{
                //首次加载
                self.statuses = array!
            }
            self.tableView.reloadData()
            
        }
        
    }
    private lazy var indicatorView: UIActivityIndicatorView = {
    
        let indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
        return indicator
    }()
    
    
}




extension HomeTableViewController {
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statuses.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellId, forIndexPath: indexPath) as! StatusCellTableViewCell
        cell.status = statuses[indexPath.row]
        if indexPath.row ==  statuses.count - 1 && !indicatorView.isAnimating()
        {
            //倒数第二行 加载更多数据  并且小菊花没有转动的时候 再加载更多数据
            indicatorView.startAnimating()
            lodata()
        }
        

        return cell
    }
}






